using UnityEngine;
using System.Collections;

public class CharacterKill : Kill 
{
	private FadeScale fade;
	private float amount;
	private bool runUpdate = true;
	private CharacterManager player;

	
	public void Initialize(int mode)
	{
		player = gameObject.GetComponent<CharacterManager>();
		foreach(Transform colliders in transform)
		{
			if(colliders.gameObject.GetComponent<BoxCollider>())
			{
				colliders.gameObject.GetComponent<BoxCollider>().enabled = false;
			}
		}
				
		amount = 1;
		player.DeactiveAll();
		player.rigidbody.isKinematic = true;
		
		killYourselfMode(mode);	
	}
	
	public override void killYourselfMode(int Mode)
	{
		switch(Mode)
		{
			case 0:
				runUpdate = true;
				StartCoroutine("run");
				break;
			
			case 1:
				fade = gameObject.AddComponent("FadeScale") as FadeScale;
				fade.fadeInitialize(FadeScale.FadeType.FADEOUT,0.7f,gameObject);
				break;
			
		}		
	}
	
	public override void actionsPostKill()
	{
		gameObject.GetComponent<CheckpointManager>().goToCheckpoint();
		
		AchievementItem ach = AchieveStorage.getInstance.getByName("Ressurection");
		ach.Add();
			
		if(ach.Teste())
		{
           	AchieveStorage.getInstance.UnlockAchieve(ach, Camera.mainCamera);
		}
		
	}

	IEnumerator run()
	{
		Camera.mainCamera.gameObject.GetComponent<Pause>().enabled = false;

		while(runUpdate)
		{
			amount -= 0.8f *Time.deltaTime;
			renderer.material.color = new Color(renderer.material.color.r,renderer.material.color.g,renderer.material.color.b,amount);
		
			if(amount <= 0)
			{
				runUpdate = false;
				StopAllCoroutines();
				actionsPostKill();
				StartCoroutine(reactivePause());
			}
			
			yield return null;
		}
		
	}
	
	IEnumerator reactivePause()
	{
		yield return new WaitForSeconds(1);
		Camera.mainCamera.gameObject.GetComponent<Pause>().enabled = true;
	}

}
